package com.diamond.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;

public class UploadUtil {
	private static final int BUFFER_SIZE = 1 * 1024;
	private static ResourceBundle res;
	public static Boolean copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				if (!dst.exists()) {
					File ccin = new File(dst.getParent());
					ccin.mkdirs();
				}
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
				return true;
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	/*
	 * 方法返回string[] 第一个参数为保存后的文件名，第二个为保存后的全文件名
	 */
	public static String[] getExtention(String srcName,String folder,String destName) {
		if(StringUtil.isEmpty(srcName))return null;
		int pos = srcName.lastIndexOf(".");
		String str=destName+srcName.substring(pos);
		if(res==null)res = ResourceBundle.getBundle("message_en");
		String myfolder = res.getString("picture_address");//保存目录地址
		if(!StringUtils.isEmpty(folder)){
			str=folder+"/"+str;
		}
		String fullFile=myfolder+"/"+str;
		String[] filenams={str,fullFile};
		return filenams;
	}
	
	/*
	 * 方法返回string[] 第一个参数为保存后的文件名，第二个为保存后的全文件名
	 */
	public static String getPicForder() {
		if(res==null)res = ResourceBundle.getBundle("message_en");
		return res.getString("picture_address");//保存目录地址
	}
	
	public static String getPicWeb() {
		if(res==null)res = ResourceBundle.getBundle("message_en");
		return res.getString("web_pic");//保存目录地址
	}
}
